package aom.ju.ss.view.pay

import android.view.View
import aom.ju.ss.R
import aom.ju.ss.databinding.PayActivityBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.AliPayMode
import aom.ju.ss.mode.LpKey
import aom.ju.ss.mode.PublicMode
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API.barter_money_pay
import aom.ju.ss.utils.http.LP_API.get_pay_info
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.pay.PayManager
import aom.ju.ss.utils.pay.WxXcxzf
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.dialog.TitleDialog
import aom.ju.ss.view.user.SetPassActivity
import com.google.gson.Gson

class PayActivity : BaseActivity() {
    private lateinit var binding: PayActivityBinding

    var goodsType = "0"////类型  1普通商品  2砍价商品  3抢券商品  4易货商品
    override fun init() {
        binding = viewBinding as PayActivityBinding
        val mode = Gson().fromJson(arr[0], WxXcxzf::class.java)

        LogUtils.e(Gson().toJson(arr))
        goodsType = arr[1]
        binding.goodsType = goodsType

        binding.yue.text = "共" + UserInfo.userInfo?.money + "可用"
        binding.yhjNum.text = "共" + UserInfo.userInfo?.barter_money + "可用"

        binding.textView30.text = mode.allPrice
        binding.textView31.djs()

        binding.type = 0
        if (goodsType == "4") {
            binding.type = 4
        }
        binding.yeLay.setOnClickListener {
            if (ckps()) {
                binding.type = 0
            }
        }
        binding.ali.setOnClickListener { binding.type = 1 }
        binding.wx.setOnClickListener { binding.type = 2 }

        binding.pay.setOnClickListener {
            when (binding.type) {
                0 -> {// 余额
                    if (ckps()) {


                        PayPassDialog2(activity!!, object : OnPayClickListener {
                            override fun onPassFinish(
                                passContent: String,
                                dialog2: PayPassDialog2?
                            ) {
                                val ps = encrypt(passContent)

                                LogUtils.e(passContent)
                                LogUtils.e(ps)
                                LogUtils.e(decrypt(ps))
                                OKHttpUtils(activity)
                                    .SetApiUrl(get_pay_info)
                                    .SetKey(
                                        "order_id",
                                        "business_type",
                                        "pay_type",
                                        "pay_id",
                                        "token",
                                        "pay_pass"
                                    ).SetValue(
                                        mode.order_id,
                                        "0",
                                        "3",
                                        mode.pay_id,
                                        UserInfo.token,
                                        ps
                                    )
                                    .POST(object : MyHttpCallBack {
                                        override fun ok(jsonString: String?, httpTY: String?) {
                                            val mode =
                                                Gson().fromJson(jsonString, PublicMode::class.java)
                                            mode.msg?.show
//                                            PayManager.aliPay(
//                                                activity,
//                                                mode.data.wxpayinfos.payinfo
//                                            )
                                            finish()
                                            SendMsg.sendEvent(LpKey.CloseOrderRelatedPage)
                                        }

                                        override fun error(
                                            e: String?,
                                            jsonString: String?,
                                            httpTY: String?
                                        ) {
                                            e?.show
                                        }

                                    })
                            }

                            override fun onPayForget(dialog2: PayPassDialog2?) {
                                start(SetPassActivity::class.java, "2")
                            }
                        })
                    }
                }
                1 -> {//支付宝
                    OKHttpUtils(activity)
                        .SetApiUrl(get_pay_info)
                        .SetKey("business_type", "pay_type", "pay_id", "token")
                        .SetValue("0", "6", mode.pay_id, UserInfo.token)
                        .POST(object : MyHttpCallBack {
                            override fun ok(jsonString: String?, httpTY: String?) {
                                val mode = Gson().fromJson(jsonString, AliPayMode::class.java)
                                PayManager.aliPay(activity, mode.data.wxpayinfos.payinfo)
                                SendMsg.sendEvent(LpKey.CloseOrderRelatedPage)
                            }

                            override fun error(e: String?, jsonString: String?, httpTY: String?) {
                                e?.show
                            }

                        })
                }
                2 -> {//微信
                    PayManager.weChatPay(
                        activity,
                        mode.api,
                        Gson().toJson(mode),
                        mode.apitype
                    )
                    SendMsg.sendEvent(LpKey.CloseOrderRelatedPage)
                    finish()
                }
                4 -> {
                    if (ckps()) {
                        PayPassDialog2(activity!!, object : OnPayClickListener {
                            override fun onPassFinish(
                                passContent: String,
                                dialog2: PayPassDialog2?
                            ) {
                                val ps = encrypt(passContent)
                                OKHttpUtils(activity)
                                    .SetApiUrl(barter_money_pay)
                                    .SetKey(
                                        "order_id",
                                        "pay_id",
                                        "pay_pass",
                                    ).SetValue(
                                        mode.order_id,
                                        mode.pay_id,
                                        ps
                                    )
                                    .POST(object : MyHttpCallBack {
                                        override fun ok(jsonString: String?, httpTY: String?) {
                                            val mode =
                                                Gson().fromJson(jsonString, PublicMode::class.java)
                                            mode.msg?.show
                                            finish()
                                            SendMsg.sendEvent(LpKey.CloseOrderRelatedPage)
                                        }

                                        override fun error(
                                            e: String?,
                                            jsonString: String?,
                                            httpTY: String?
                                        ) {
                                            e?.show
                                        }

                                    })
                            }

                            override fun onPayForget(dialog2: PayPassDialog2?) {
                                start(SetPassActivity::class.java, "2")
                            }
                        })
                    }
                }
            }
        }

    }

    private fun ckps(): Boolean {
        if (UserInfo.userInfo!!.isset_pay_password == "0") {
            TitleDialog().show(activity, object : TitleDialogInterface {
                override fun ok(view: View?, IsDetermine: Boolean) {
                    if (IsDetermine) {
                        start(SetPassActivity::class.java, "2")
                    }
                }
            }).setConnect("尚未支付设置密码")
                .tvDetermine("去设置")

            return false
        }
        return true
    }

    override val lay: Int = R.layout.pay_activity


}